package com.nzf.vote.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nzf.vote.entity.Type;
import com.nzf.vote.model.vo.Results;
import com.nzf.vote.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author nzf
 * @since 2022-07-25
 */
@CrossOrigin
@RestController
@RequestMapping("/type")
public class TypeController {
    @Autowired
    ITypeService typeService;

    @GetMapping("getTypeList")
    public Results getTypeList(){
        return Results.success(typeService.list());
    }

    @PostMapping("insertType")
    public Results insertType(@RequestBody Type type){
        LocalDateTime dateTime = LocalDateTime.now();
        type.setCreateTime(dateTime);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("type",type.getType());
        Type one = typeService.getOne(queryWrapper);
        if(one==null){
            boolean save = typeService.save(type);
            return Results.success(save);
        }else{
            return new Results(500,"添加的类别已存在");
        }
    }

    @GetMapping("getTypeDtoList")
    public Results getTypeDtoList(){
        return Results.success(typeService.getTypeDtoList());
    }

    @DeleteMapping("deleteType/{id}")
    public Results deleteType (@PathVariable Integer id){
        return Results.success(typeService.removeById(id));
    }

}
